import org.apache.poi.ss.formula.functions.Column;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WGL
 * Date: 2023-01-18
 * Time: 0:20
 */
public class ShowInf {

    public void showSpecialityStudentInf(Speciality[] specialities, InfCollector[] infCollectors, Student[] toTest, ArrayList<EnrollmentTeam> toRecruit) {
        try (Workbook workbook = new SXSSFWorkbook();
             FileOutputStream out = new FileOutputStream("E:\\RecruitStudent5.0\\result.xlsx")) {

            // 创建新的工作簿
            Sheet sheet = workbook.createSheet("1");
            Row row = sheet.createRow(0);
            Cell cell1 = row.createCell(1);
            cell1.setCellValue("最低排位");
            Cell cell2 = row.createCell(2);
            cell2.setCellValue("最高排位");
            Cell cell3 = row.createCell(3);
            cell3.setCellValue("模拟平均排位");
            Cell cell4 = row.createCell(4);
            cell4.setCellValue("真实平均排位");
            Cell cell5 = row.createCell(5);
            cell5.setCellValue("真实最低排位");


            for (int i = 0; i < specialities.length; i++) {
                Row row1 = sheet.createRow(i + 1);
                int sum = 0;
                while (!specialities[i].queue.isEmpty()) {
                    assert specialities[i].queue.peek() != null;
                    sum += specialities[i].queue.peek().rank;
                    specialities[i].officiallyRecruitedStudent.add(specialities[i].queue.poll());

                }
                Cell cell = row1.createCell(0);
                cell.setCellValue(specialities[i].name);
                System.out.print(specialities[i].name);
                cell = row1.createCell(1);
                if ((specialities[i].officiallyRecruitedStudent.size() > 0)) {
                    cell.setCellValue(specialities[i].officiallyRecruitedStudent.get(0).rank);
                    cell = row1.createCell(2);
                    cell.setCellValue(specialities[i].officiallyRecruitedStudent.get(specialities[i].officiallyRecruitedStudent.size() - 1).rank);
                    cell = row1.createCell(3);
                    cell.setCellValue((float) sum / specialities[i].officiallyRecruitedStudent.size());
                    cell = row1.createCell(4);
                    cell.setCellValue(specialities[i].exactNum);
                    cell = row1.createCell(5);
                    cell.setCellValue(specialities[i].lowestRank);
                    System.out.print(" " + specialities[i].officiallyRecruitedStudent.get(0).rank + " " + specialities[i].officiallyRecruitedStudent.get(specialities[i].officiallyRecruitedStudent.size() - 1).rank);
                    System.out.print(" " + sum / specialities[i].officiallyRecruitedStudent.size());
                    cell = row1.createCell(6);
                    if (specialities[i].officiallyRecruitedStudent.size() == specialities[i].numToRecruit) {
                        System.out.print(" 招满了");
                        cell.setCellValue("招满");
                    }
                    if (specialities[i].officiallyRecruitedStudent.size() > specialities[i].numToRecruit) {
                        System.out.print(" 怎么他妈的招多了");
                    }
                    if (specialities[i].officiallyRecruitedStudent.size() < specialities[i].numToRecruit) {
                        cell.setCellValue((specialities[i].officiallyRecruitedStudent.size() - specialities[i].numToRecruit));
                        System.out.print(" 未招满,少招了" + (specialities[i].numToRecruit - specialities[i].officiallyRecruitedStudent.size()) + "人 " + "招生目标: " + specialities[i].numToRecruit);
                    }
                    System.out.println(" ");

                    infCollectors[i].highestRank += specialities[i].officiallyRecruitedStudent.get(0).rank;
                    infCollectors[i].lowestRank += specialities[i].officiallyRecruitedStudent.get(specialities[i].officiallyRecruitedStudent.size() - 1).rank;
                    infCollectors[i].avgRank += sum / specialities[i].officiallyRecruitedStudent.size();

                } else {
                    cell.setCellValue("怎么没招到学生?");
                }

            }
            // 获取格式编码值
            workbook.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try (Workbook workbook = new SXSSFWorkbook();
             FileOutputStream out = new FileOutputStream("E:\\RecruitStudent5.0\\abort.xlsx")) {
            Sheet sheet = workbook.createSheet("1");
            ArrayList<Integer> tag = new ArrayList<>();
            for (int i = 0; i < toRecruit.size(); i++) {
                Row row = sheet.createRow(i);
                Cell cell = row.createCell(0);
                cell.setCellValue(toRecruit.get(i).name);
                for (int j = 0; j < toRecruit.get(i).abortStudentRank.size(); j++) {
                    tag.add(toRecruit.get(i).abortStudentRank.get(j));
                    cell = row.createCell(j + 1);
                    cell.setCellValue(toRecruit.get(i).abortStudentRank.get(j));
                }
            }
            Row row = sheet.createRow(toRecruit.size());
            Cell cell = row.createCell(0);
            cell.setCellValue("总体跑路情况");
            for (int i = 0; i < tag.size(); i++) {
                cell = row.createCell(i + 1);
                cell.setCellValue(tag.get(i));
            }
            workbook.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int sum = 0;
        for (int i = 0; i < specialities.length; i++) {
            sum += specialities[i].numToRecruit;
        }
        System.out.println("目标招生人数:" + sum);
        sum = 0;
        for (int i = 0; i < toTest.length; i++) {
            if (toTest[i].isRecruitedGdut) sum++;
        }
        System.out.println("模拟仿真人数:" + toTest.length);
        System.out.println("模拟仿真录取人数:" + sum);

//        try (Workbook workbook = new SXSSFWorkbook();
//             FileOutputStream out = new FileOutputStream("E:\\RecruitStudent5.0\\test.xlsx")) {
//            Sheet sheet = workbook.createSheet("1");
//            for (int i = 0; i < specialities.length; i++) {
//                Row row = sheet.getRow(i*3);
//                Cell cell1 = row.createCell(0);
//                cell1.setCellValue(specialities[i].name+"学院");
//                if(specialities[i].officiallyRecruitedStudent.size()>0){
//                    for (int j = 0; j < specialities[i].officiallyRecruitedStudent.size(); j++) {
//                        Cell cell = row.createCell(j+1);
//                        cell.setCellValue(specialities[i].officiallyRecruitedStudent.get(j).rank);
//                    }
//                }else {
//                    Cell cell = row.createCell(0);
//                    cell.setCellValue("他妈的有问题啊");
//                }
//
//            }
//            workbook.write(out);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    public void showSpecialityInTUmu(Speciality[] specialities,InfCollector[] infCollectors){
        for (int i = 0; i < specialities.length; i++) {
            if (specialities[i].academy.equals("土木与交通工程学院")){
                String name = specialities[i].name;
                for (int j = 0; j < infCollectors.length; j++) {
                    if (name.equals(infCollectors[j].name)){
                        int progress = specialities[i].exactNum-infCollectors[j].avgRank;
                        System.out.println(name + " 平均录取排名提升:"+progress);
                    }
                }
            }
        }
    }

    public void showAcademyLos(InfCollector[] infCollectors,Speciality[] specialities,String[] academy){
        ArrayList<String> changes = new ArrayList<>();
        for (int i = 0; i < academy.length; i++) {
            System.out.println(academy[i]+": ");
            String academyName = academy[i];
            int numInAcademy = 0;
            int sum = 0;
            for ( int j =0 ; j < specialities.length; j++) {
                if (specialities[j].academy.equals(academyName)){
                    numInAcademy++;
                    for (int i1 = 0; i1 < infCollectors.length; i1++) {
                        if(infCollectors[i1].name.equals(specialities[j].name)){
                            int progress = specialities[j].exactNum-infCollectors[i1].avgRank;
                            System.out.println(specialities[j].name+" 平均录取排名变化:"+progress);
                            sum+=progress;
                        }
                    }
                }
            }
            System.out.println( academyName+"整体录取排名变化: "+sum/numInAcademy);
            String change = academyName+"录取排名变化: "+sum/numInAcademy;
            changes.add(change);
            System.out.println(" ");
        }
        for (int i = 0; i < changes.size(); i++) {
            System.out.println(changes.get(i));
        }
        System.out.println("======================================================================");
        
    }

    public void showChuangxing(InfCollector[] infCollectors,Speciality[] specialities){
        for (int i = 0; i < specialities.length; i++) {
            if (specialities[i].name.contains("创新班")){
                for (int i1 = 0; i1 < infCollectors.length; i1++) {
                    if (infCollectors[i1].name.equals(specialities[i].name)){
                        int progress = specialities[i].exactNum-infCollectors[i1].avgRank;
                        System.out.println(specialities[i].name+" 平均录取排名变化:"+progress);
                    }
                }
            }
        }
    }
}